package com.tangcco.lockwell.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.domain.User;
import com.tangcco.lockwell.repository.UserMapper;
import com.tangcco.lockwell.service.UserService;
import com.tangcco.lockwell.utility.StudentSubjectUtil;
import com.tangcco.lockwell.viewmodel.admin.subject.SubjectVM;
import com.tangcco.lockwell.viewmodel.admin.user.UserLearningProgress;
import com.tangcco.lockwell.viewmodel.admin.user.UserPageRequestVM;
import com.tangcco.lockwell.viewmodel.admin.user.UserProgress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.annotation.ElementType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    private final UserMapper userMapper;
    private final ApplicationEventPublisher eventPublisher;

    @Resource
    SubjectServiceImpl subjectService;
    @Resource
    StudentSubjectUtil studentSubjectUtil;

    @Autowired
    public UserServiceImpl(UserMapper userMapper, ApplicationEventPublisher eventPublisher) {
        super(userMapper);
        this.userMapper = userMapper;
        this.eventPublisher = eventPublisher;
    }

    //用户的添加
    @Override
    public int insert(User user) {
        return userMapper.insertSelective(user);
    }
    @Override
    public int insertByFilter(User record) {
        return userMapper.insertSelective(record);
    }
//查询用户
    @Override
    public User selectById(Integer id) {
        return userMapper.getUserById(id);
    }
    @Override
    public int updateByIdFilter(User record) {
        return super.updateByIdFilter(record);
    }
    @Override
    public List<User> getAllUser(UserPageRequestVM userPageRequestVM) {
        return userMapper.getAllUser(userPageRequestVM);
    }
    @Override
    public User getUserById(Integer userId) {
        return userMapper.getUserById(userId);
    }
    @Override
    public User getUserByUserName(String username) {
        return  userMapper.getUserByUserName(username);
    }
    @Override
    public User getUserByPhone(String phone) {
        return  userMapper.getUserByPhone(phone);
    }


    @Override
    public List<User> userPageList(Map<String, Object> map) {
        return Collections.emptyList();
    }



    @Override
    public List<User> userPage(UserPageRequestVM requestVM) {
        PageInfo<User> pageInfo = PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(),
                "user_id desc").doSelectPageInfo(() ->
                userMapper.userPage(requestVM));
        return pageInfo.getList();
    }



    @Override
    public List<User> selectByIds(List<Integer> ids) {
        return Collections.emptyList();
    }

    @Override
    public User selectByWxOpenId(String wxOpenId) {
        return null;
    }

    @Override
    public int deleteByIdStatus(Integer userId) {
        return userMapper.deleteByIdStatus(userId);
    }

    @Override
    public Integer selectAdminCount() {
        return userMapper.selectAdminCount();
    }

    @Override
    public Integer selectStudentCount() {
        return userMapper.selectStudentCount();
    }




    @Override
    public void insertUser(User user) {
    }

    @Override
    public void insertUsers(List<User> users) {
    }

    @Override
    public void updateUser(User user) {

    }

    @Override
    public void updateUsersAge(Map<String, Object> map) {

    }
    @Override
    public Integer userPageCount(Map<String, Object> map) {
        return 0;
    }
    @Override
    public Integer selectAllCount() {
        return 0;
    }

    @Override
    public User getUserByUuid(String uuid) {
        return null;
    }
    @Override
    public int updateById(User record) {
        return 0;
    }


    @Override
    public User getUserByUserNamePwd(String username, String pwd) {
        return null;
    }


    @Override
    public List<UserLearningProgress> selectProgressByUserName(User user) {
        //根据用户Id查询用户数据
        if (user == null){
            return null;
        }
        Integer userId = user.getUserId();
        String optSubject = user.getOptSubject();
        List<Integer> subjectIds = new ArrayList<>();
        if (optSubject != null){
            String[] split = optSubject.split(",");
            for (String s : split){
                subjectIds.add(Integer.parseInt(s));
            }
        } else {
            return null;
        }
        UserProgress userProgress = new UserProgress(userId,subjectIds);
        List<UserLearningProgress> userLearningProgresses = userMapper.selectLearningProgress(userProgress);
        return userLearningProgresses;
    }

    @Override
    public List<UserLearningProgress> selectAllLearningProgress(User user) {
        Integer userId = null;
        List<Integer> subjectIds = new ArrayList<>();
        if (user != null){
            userId = user.getUserId();
            String optSubject = user.getOptSubject();
            if (optSubject == null){
                List<SubjectVM> subjectVMS = subjectService.selectAllMainSubject();
                for (SubjectVM subjectVM : subjectVMS){
                    subjectIds.add(subjectVM.getId());
                }
            } else {
                String[] split = optSubject.split(",");
                for (String s : split){
                    subjectIds.add(Integer.parseInt(s));
                }
            }
        }else {
            return null;
        }
        UserProgress userProgress = new UserProgress(userId,subjectIds);
        return userMapper.selectAllLearningProgress(userProgress);
    }

}
